#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

void signal_handler(int sig)
{
    printf("handler : sig = %d\n", sig);
}

int r_read(char* data, int len)
{
    int ret = -1;
    while(data && (ret = read(STDIN_FILENO, data, len-1)) == -1 && errno == EINTR) {
        printf("restart syscall mannually...\n");
    }
    
    if( ret != -1 ) {
        data[ret] = 0;
    }
    return ret;
}

int main(void)
{
    char buf[32] = {0};
    struct sigaction act = {0};
    
    act.sa_handler = signal_handler;
    act.sa_flags = SA_RESTART | SA_RESETHAND;
    
    sigaction(SIGINT, &act, NULL);
    
    r_read(buf, 32);
    
    printf("input: %s\n", buf);
    
    return 0;
}

